package impl

import (
	"context"
	"github.com/starmotioncf/common/lang"
	"github.com/starmotioncf/soutchatinternal/model"
	"github.com/starmotioncf/soutchatinternal/repository"
)

type langRepository struct {
	*repository.Repository
}

func NewLangRepository(r *repository.Repository) repository.LangRepository {
	return &langRepository{
		Repository: r,
	}
}

func (l *langRepository) Get(ctx context.Context, key string) *lang.I18n {
	var m *model.Lang
	err := l.ReadDB(ctx).Where("key = ?", key).First(&m).Error
	if err != nil {
		return lang.NewI18nWithKey(key, nil)
	}
	return lang.NewI18nWithKey(key, map[string]string{
		m.Key: m.Val,
	})
}
